<?php


namespace app\admin\model;


class ProtectPaymentModel extends BaseModel
{
    protected $name='protect_payment';

    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    protected $createTime = 'create_time';

    public function doAddData($mw,$parem=[]){

        $this->startTrans();
        try {
            $data = array(
                'order_id' => $mw['order_id'],
                'customer_id' => $mw['customer_id'],
                'mw_num' => $mw['tomb_num'],
                'mw_address' => $mw['tomb_address'],
                'dealer' => $parem['dealer'],
                'sex' => $parem['sex'],
                'phone' => $parem['phone'],
                'relation' => $parem['relation'],
                'card_ytpe' => $parem['card_ytpe'],
                'card_file' => $parem['card_file'],
                'cart_num' => $parem['cart_num'],
                'address' => $parem['address'],
                'manager_price' => $parem['manager_price'],
                'duration' => $parem['duration'],
                'free_reduce' => $parem['free_reduce'],
                'total_price' => $parem['total_price'],
                'pay_type' => $parem['pay_type'],
                'dealers' => $parem['dealers'],
                'pay_time' => strtotime($parem['pay_time']),
                'handle_name' => cmf_get_admin_name(),
                'mw_id' => $mw['tomb_id'],
                'end_time' => unixtime_to_date_inc(time(),$parem['duration']),
            );
            $id_info  = $this->create($data);

            $other_id= $id_info['id'];
            $pay_ins = [
                'type'=>3,
                'itype'=>4,
                'other_id'=>$other_id,
                'order_id'=>0 ,
                'pay_sn'=>OrderPayLogModel::getselfPayNum('add_time','pay_sn','JS-'),// 'DJ'.date('YmdHi'),
                'customer_id'=>$mw['customer_id'],
                'tomb_id'=>$mw['tomb_id'],
                'tomb_num'=>$mw['tomb_num'],
                'tom_price'=>$parem['manager_price'],//
                'free_price'=>$parem['free_reduce'],//
                'deal_price'=>$parem['total_price'],//
                'total_price'=>$parem['total_price'],//
                'actual_amount'=>$parem['manager_price']*$parem['duration'],//
                'handle'=>cmf_get_current_admin_id(),//付款人
                'real_name'=>$mw['buyer_name'],//付款人
                'real_phone'=>$mw['buyer_phone'],//付款人
                'pay_type'=>$parem['pay_type'],//付款方式
                'status'=> 0,// 付款方式
                'add_time' => time(),
            ];
            $rets =  OrderPayLogModel::insertGetId($pay_ins);
            if (!$rets){
                $this->rollback();
                $this->error = '操作错误';
                return false;
            }
            $pay_id = $rets;

            // 发送短信
            if (isset($parem['is_sms'])){
                //发送短信
                $iSenf = sendNoticeSms(5,$parem['phone'],['tomb_num'=>$mw['tomb_num']],$errorMsg);
                if ($iSenf==false){
                    return $this->error($errorMsg);
                }
            }


            $this->commit();
            return true;
        }catch(\Exception $exception) {
            $this->rollback();
            $this->error = '操作错误';
            return false;
        }
    }
}